﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class InvoiceEntry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (((UserVO)Session["CurrentUserVO"]) == null)
            //{
            //    Response.Redirect("ReconSignIn.aspx");
            //}
           User user =  ((User)Session["CurrentUserVO"]);
           //populate remitter list
           RemitterEntity remitEntity = new RemitterEntity();
           List<Remitter> remitters = remitEntity.GetRemitterIdForMatchingCriteria(user.CustomerID);
           ddlRemitter.DataSource = remitters;
           ddlRemitter.DataBind();

           //if remitter type user then select remitter and disable selection
           if (user.UserType == "R")
           {
               foreach (ListItem item in ddlRemitter.Items)
               {
                   if (item.Value == user.RemitterID)
                   {
                       item.Selected = true;
                       break;
                   }
               }
               ddlRemitter.Enabled = false;
           }
        }
        protected void btnInvoiceCancel_Click(object sender, EventArgs e)
        {
            ResetFieldsInvoice();
        }

        protected void btnShowPopup_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
        }
        protected void btnInvoice_Click(object sender, EventArgs e)
        {
            User user = ((User)Session["CurrentUserVO"]);
           
            System.Globalization.CultureInfo enGB = new System.Globalization.CultureInfo("en-GB");
            InvoiceEntity invEntity = new InvoiceEntity();
            Invoice inv = new Invoice
            {
                InvoiceNumber = txtInvoiceNo.Text.Trim(),
                //InvoiceDate = Convert.ToDateTime(ConvertDate(txtInvoiceDate.Value)),
                InvoiceDate = DateTime.ParseExact(txtInvoiceDate.Value, "d-M-yyyy", enGB),
                InvoiceDueDate = DateTime.ParseExact(txtInvoiceDueDate.Value, "d-M-yyyy", enGB),
                RemitterID = ddlRemitter.SelectedValue,//txtRemitterId.Text.Trim(),
                CustomerID = user.CustomerID,//txtICustomerId.Text.Trim(),
                Currency = "INR",//txtCurrency.Text.Trim(),
                Amount = Convert.ToDouble(txtIAmount.Text.Trim()),
                PORef = txtIRefNoChqNo.Text.Trim(),
                InvoiceStatus = "N",
                IsPartial = false 
            };
            int retval = invEntity.AddInvoice(inv);

            if (retval != 0)
            {
                lblMessage.Text = "Invoice added successfully!";
                lblJqueryTest.Text = "Invoice added successfully!";
                //btnClickPopup.Show();
                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Invoice Entry');", true);
                ResetFieldsInvoice();
                
            }
            else
            {
                lblMessage.Text = "Error adding Invoice!";
                lblJqueryTest.Text = "Error adding Invoice!";
                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Invoice Entry');", true);
              //  btnClickPopup.Show();
            }
        }
       
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        public string ConvertDate(string yyyyMMdd)
        {
            string[] str;
            string s = yyyyMMdd;
            str = s.Split('-').ToArray();

            if (str[0].Length == 1)
            { str[0] = "0" + str[0]; }

            if (str[1].Length == 1)
            { str[1] = "0" + str[1]; }

                s = str[2] + "-" + str[1] +  "-" + str[0];
                return s;

        }

        private void ResetFields()
        {
            
        }

        private void ResetFieldsInvoice()
        {      
            //txtICustomerId.Text = "";
            //txtRemitterId.Text = "";
            txtInvoiceDate.Value = "";
            txtInvoiceNo.Text = "";
            //txtCurrency.Text = "";
            txtIAmount.Text = "";
            txtIRefNoChqNo.Text = "";
           // chkIsPartialInvoice.Checked = false;
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtStatementDt.ClientID + "','cal','width=250,height=225,left=480,top=280')</script>");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtPostingDt.ClientID + "','cal','width=250,height=225,left=750,top=310')</script>");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtValueDt.ClientID + "','cal','width=250,height=225,left=480,top=310')</script>");
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtInvoiceDate.ClientID + "','cal','width=250,height=225,left=750,top=310')</script>");
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        
    }
}